package com.jcl.cloud.services.login.service;


import com.jcl.cloud.services.login.config.RedisConfigurationProperties;
import com.jcl.cloud.services.unicrawler.common.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.*;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class RedisService {

    @Autowired
    private RedisConfigurationProperties redisConfigurationProperties;

    private Jedis jedis;

    @PostConstruct
    public void init(){
        JedisPool jedisPool = null;
        if (this.redisConfigurationProperties.getAddressList().size() > 1) {
            return ;
        } else {
            String hostPort = (String)this.redisConfigurationProperties.getAddressList().get(0);
            String[] hp = hostPort.split(":");
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(this.redisConfigurationProperties.getMaxTotal());
            config.setMaxIdle(this.redisConfigurationProperties.getMaxIdle());
            config.setMinIdle(this.redisConfigurationProperties.getMinIdle());
            config.setMaxWaitMillis(this.redisConfigurationProperties.getMaxWaitMillis());
            jedisPool = new JedisPool(config, hp[0], Integer.parseInt(hp[1]), 2000, null, this.redisConfigurationProperties.getDbIndex());
        }

            JedisCommands commands = JedisUtil.getJedisCommands(null,jedisPool);
            this.jedis = (Jedis)commands;
        System.out.println("RedisService启动成功...");
    }



    public void lpush(String key,String value){
        this.jedis.lpush(key,value);
    }



    public void del(String key){
        this.jedis.del(key);

    }


    public void hset(String key,String field,String value){
        this.jedis.hset(key,field,value);
    }


    public String hget(String key,String field){
        return this.jedis.hget(key,field);


    }

    public List hmget(String var1, String... var2){
        return this.jedis.hmget(var1,var2);
    }


    public void set(String key,String value){



        this.jedis.set(key,value);
    }


    public void hdel(String key,String field){

        this.jedis.hdel(key,field);
    }

}
